//
//  HistoryCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/27/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit

class HistoryCell: UITableViewCell {
    
    // MARK: - Outlets
    
    lazy var placeholderLbl: UILabel = {
        let lbl = UILabel()
        lbl.numberOfLines = 1
        lbl.font = UIFont.systemFont(ofSize: 12, weight: .regular)
        lbl.textAlignment = .left
        return lbl
    }()
    
    lazy var priceLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.systemFont(ofSize: 17, weight: .semibold)
        lbl.numberOfLines = 1
        lbl.textColor = .black
        lbl.textAlignment = .left
        return lbl
    }()
    
    // MARK: - Properties
    
    private var historyItem: HistoryItem! {
        didSet {
            self.setPlaceholderLblText()
            self.priceLbl.text = historyItem.cost
        }
    }
    
    // MARK: - Init
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.accessoryType = .disclosureIndicator
        self.addViews()
    }

    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func configure (with historyItem: HistoryItem) {
        self.historyItem = historyItem
    }
    
    // MARK: - UI
    
        /// Initial: adding views to superview
    private func addViews() {
        self.contentView.addSubview(placeholderLbl)
        self.contentView.addSubview(priceLbl)
        
        placeholderLbl.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalToSuperview().inset(8)
            make.height.equalTo(24)
        }
        
        priceLbl.snp.makeConstraints { (make) in
            make.height.equalTo(30)
            make.top.equalTo(placeholderLbl.snp.bottom).offset(3)
            make.leading.trailing.equalToSuperview().inset(16)
        }
    }
        /// Set Placeholder text with mixed font weights
    private func setPlaceholderLblText () {
        let boldAttrs = [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 12)]
        let boldStr = NSMutableAttributedString(string: "№\(historyItem.id)", attributes: boldAttrs)
        
        let regulatAttrs = [NSAttributedString.Key.foregroundColor: UIColor.greyColor]
        
        let regularStr = NSMutableAttributedString(string: "Объявление ", attributes: regulatAttrs)
        regularStr.append(boldStr)
        self.placeholderLbl.attributedText = regularStr
    }
}

